package com.zh.jbet.core.tournament.entity;

import java.util.Date;
import java.util.List;

import com.zh.jbet.core.entity.Game;
import com.zh.jbet.core.scorer.TargetBets;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Transient;

@TargetBets(bets={MatchResultBet.class, TeamsBet.class})
@Entity
@Table(name = "TEAMMATCH")
public class TeamMatch implements Game, Comparable<TeamMatch>, Serializable {

    private static final String DELIM = " - ";
    private Long rootId;
    private Long parentId;
    private Long id;
    private Team home;
    private Team away;
    private Date startTime;
    private List<MatchResult> results = new ArrayList<MatchResult>(3);

    @Id
    @SequenceGenerator(name = "S_TEAMMATCH", sequenceName = "S_TEAMMATCH", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_TEAMMATCH")
    @Override
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Basic
    @Override
    public Long getParentId() {
        return parentId;
    }
    @Override
    public void setParentId(Long parentId) {
        this.parentId = parentId;
    }

    @Basic
    @Override
    public Long getRootId() {
        return rootId;
    }
    @Override
    public void setRootId(Long rootId) {
        this.rootId = rootId;
    }
    
    @ManyToOne()
    @JoinColumn(name="HOMEID")
    public Team getHome() {
        return home;
    }
    public void setHome(Team home) {
        this.home = home;
    }
    @ManyToOne()
    @JoinColumn(name="AWAYID")
    public Team getAway() {
        return away;
    }
    public void setAway(Team away) {
        this.away = away;
    }
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Basic
    @Override
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    @OneToMany(mappedBy="game",cascade=CascadeType.ALL)
    @OrderBy("typeIdx ASC")
    public List<MatchResult> getResults() {
        return results;
    }
    public void setResults(List<MatchResult> results) {
        this.results = results;
    }

    @Transient
    @Override
    public String getShortName() {
        return home.getShortName() + DELIM + away.getShortName();
    }
    @Transient
    @Override
    public String getName() {
        return home.getName() + DELIM + away.getName();
    }

    @Transient
    public MatchResult getResult() {
        return results.size() > 0 ? results.get(0) : null;
    }

    @Transient
    public String getResultString() {
        StringBuilder sb = new StringBuilder(20);
        Iterator<MatchResult> result = results.iterator();
        while (result.hasNext()) {
            sb.append(result.next().toString());
            if (result.hasNext()) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }
    
    @Transient
    @Override
    public Boolean isFinished() {
        return getResult() != null;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TeamMatch other = (TeamMatch) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 17 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public int compareTo(TeamMatch o) {
        if (o != null && o.getStartTime() != null) {
            return o.getStartTime().compareTo(startTime);
        } else {
            return -1;
        }
    }
}
